import Vue from 'vue'
import Router from 'vue-router'
import { Store } from 'vuex'
import HelloWorld from './components/HelloWorld'
import userRouters from './module/user'

Vue.use(Router)

export default new Router({
  model:"history",
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    ...userRouters,
  ]
})

router.beforeEach((to, from, next) => {  //路由守卫
  // to and from are both route objects. must call `next`.
  if (to.meta.auth) {  //判断用户是否登录
    if (Store.state.userModule.token) { //判断用户的token是否存在
      //还需要判断token的有效性  比如是否有没有过期  所以需要后端发放token时带上token的有效期
      //token无效则请求token
      
      next()
    }
  } else {
    //跳转登录
    router.push({name:'login'})
  }
})  